import { loading } from '../../utils/util'
import { _getTips } from '../../common/api/signin'
import computedBehavior from 'miniprogram-computed'
const app = getApp()
Component({
  behaviors: [computedBehavior],
  data: {
    userinfo: {},
    alert: false,
    tipShow: false,
    tips: {}
  },
  computed: {},
  methods: {
    showAct () {
      this.setData({
        alert: true
      })
      // let itemList = ['模拟考试', '正式考试']
      // let itemColor = ['#f00', '#333']
      // wx.showActionSheet({
      //   itemList,
      //   success: res => {
      //     let type = itemList[res.tapIndex]
      //     wx.navigateTo({
      //       url: `/pages/testing/index?type=${type}`
      //     })
      //   },
      //   fail: err => {
      //     console.log(err)
      //   }
      // })
    },
    hideAct () {
      this.setData({
        alert: false
      })
    },
    showTip () {
      this.setData({
        tipShow: true
      })
    },
    hideTip() {
      this.setData({
        tipShow: false
      })
    },
    getTips () {
      app.loading('加载中')
      let memberid = wx.getStorageSync('memberid')
      _getTips(memberid).then(res => {
        console.log(res)
        wx.hideLoading()
        if (res.data.IsSuccess) {
          console.log(res.data.Data)
          this.setData({
            tips: {
              tit: res.data.Msg,
              img: res.data.Data
            }
          }, () => {
            this.showTip()
          })
        }
      }).catch(err => {
        console.log(err)
        wx.hideLoading()
        wx.showModal({
          title: '对不起',
          content: '请求错误，请稍后再试',
          showCancel: false
        })
      })
    },
    logout () {
      console.log('logout')
      wx.removeStorage({
        key: 'memberid',
        success: res => {
          wx.showModal({
            title: '温馨提示',
            content: '登出成功！',
            showCancel: false,
            success: r => {
              wx.redirectTo({
                url: '/pages/signin/index'
              })
            }
          })
        }
      })
    },
    onLoad() {},
    onShow() {
      if (app.store.userinfo) {
        this.data.userinfo = app.store.userinfo
        this.getTips()
      } else {
        loading('加载中')
        app.launch().then(res => {
          wx.hideLoading()
          console.log(res)
          this.data.userinfo = res
          this.getTips()
        }).catch(err => {
          wx.hideLoading()
          console.log(err)
        })
      }
    }
  }
})
